/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = wtc_tcb_tdomvals.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2015/08/27 by songbb                               */
/*******************************************************************/

#include "conntrack.h"

#include "../wtc_core.h"
#include "../wtc_comm.h"
#include "../wtc_mar.h"
#include "wtc_tcb_tdomvals.h"

int wtc_tcb_tdomvals_tmpostrecv(Conntrack *p_st_conn
                            , Apm_packet *p_analyze_in
                            , L7session_wtc *p_st_sess
                            , L7_wtc_packet *p_st_wtc_packet
                            , int i_recv_size
                            , int i_hint_index)
{
    char arr_scratch[150] = {'\0'};
    unsigned int ui_dv_version = 0;
    unsigned int ui_dv_descrim = 0;
    char *p_dv_srvc = NULL;
    unsigned int ui_dv_srvc_len = 0;
    char *p_dv_src_domain = NULL;
    unsigned int ui_dv_src_domain_len = 0;
    char *p_dv_orig_domain = NULL;
    unsigned int ui_dv_orig_domain_len = 0;
    char *p_dv_dest_domain = NULL;
    unsigned int ui_dv_dest_domain_len = 0;

    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_dv_version, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_dv_descrim, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    if (ui_dv_descrim == 1)
    {
        if (wtc_mar_decode_string(p_st_sess, arr_scratch, 150, p_st_wtc_packet, &p_dv_srvc, &ui_dv_srvc_len) != FR_SUCC)
            goto error;
        WTC_PRINT_FATAL("p_dv_srvc:[%s]\n", p_dv_srvc);
    }
    else
    {
        if (wtc_mar_decode_string(p_st_sess, arr_scratch, 150, p_st_wtc_packet, &p_dv_src_domain, &ui_dv_src_domain_len) != FR_SUCC)
            goto error;
        if (wtc_mar_decode_string(p_st_sess, arr_scratch, 150, p_st_wtc_packet, &p_dv_orig_domain, &ui_dv_orig_domain_len) != FR_SUCC)
            goto error;
        if (wtc_mar_decode_string(p_st_sess, arr_scratch, 150, p_st_wtc_packet, &p_dv_dest_domain, &ui_dv_dest_domain_len) != FR_SUCC)
            goto error;
        WTC_PRINT_FATAL("p_dv_src_domain:[%s]\n", p_dv_src_domain);
        WTC_PRINT_FATAL("p_dv_orig_domain:[%s]\n", p_dv_orig_domain);
        WTC_PRINT_FATAL("p_dv_dest_domain:[%s]\n", p_dv_dest_domain);
    }

    wtc_free((void **) &p_dv_srvc);
    wtc_free((void **) &p_dv_src_domain);
    wtc_free((void **) &p_dv_orig_domain);
    wtc_free((void **) &p_dv_dest_domain);
    return FR_SUCC;
error:
    wtc_free((void **) &p_dv_srvc);
    wtc_free((void **) &p_dv_src_domain);
    wtc_free((void **) &p_dv_orig_domain);
    wtc_free((void **) &p_dv_dest_domain);
    return FR_FAIL;
}

int wtc_tcb_tdomvals_tmpostrecv_fad(char *p_payload
                                    , unsigned int ui_payload_len
                                    , unsigned int *p_ui_offset
                                    , int *p_i_direction
                                    , int i_recv_size
                                    , int i_hint_index)
{
    unsigned int ui_dv_descrim = 0;

    if (wtc_skip_int(p_payload, ui_payload_len, p_ui_offset) != FR_SUCC)
        goto error;
    if (wtc_get_int(p_payload, ui_payload_len, p_ui_offset, &ui_dv_descrim) != FR_SUCC)
        goto error;
    if (ui_dv_descrim == 1)
    {
        if (wtc_skip_string(p_payload, ui_payload_len, p_ui_offset) != FR_SUCC)
            goto error;
    }
    else
    {
        if (wtc_skip_string(p_payload, ui_payload_len, p_ui_offset) != FR_SUCC)
            goto error;
        if (wtc_skip_string(p_payload, ui_payload_len, p_ui_offset) != FR_SUCC)
            goto error;
        if (wtc_skip_string(p_payload, ui_payload_len, p_ui_offset) != FR_SUCC)
            goto error;
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}
